package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.NewsDto;
import primetest.faces.NewsListModel;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.NewsService;

@Named("newsListControler")
@SessionScoped
public class NewsListControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -2341239310873546662L;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Inject
	private SessionManager sessionManager;

	private NewsListModel newsList = null;
	private List<NewsDto> baseNewsList;
	private NewsDto selectedNews = new NewsDto();
	private NewsDto backNews = new NewsDto();
	private NewsDto newNews = new NewsDto();
	private int first;
	private boolean editMode = false;
	private boolean addMode = false;

	private List<NewsDto> filteredTerms;

	public boolean getEditMode() {
		return editMode;
	}

	public boolean getAddMode() {
		return addMode;
	}

	@Inject
	private NewsService newsService;

	public NewsListModel getNewsList() {
		if (baseNewsList == null) {
			try {
				baseNewsList = newsService.getNewsList();
				newsList = new NewsListModel(baseNewsList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseNewsList == null ? new NewsListModel(
				new ArrayList<NewsDto>()) : newsList;
	}

	public void deleteNews() throws IOException {
		newsService.deleteNews(selectedNews.getKey());
	}

	public void reload() {
		baseNewsList = null;
	}

	public NewsDto getSelectedNews() {
		return selectedNews;
	}

	public void setSelectedNews(NewsDto selectedNews) {
		this.selectedNews = selectedNews;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public NewsDto getNewNews() {
		return newNews;
	}

	public void setNewNews(NewsDto newNews) {
		this.newNews = newNews;
	}

	public void startEditNews() {
		editMode = true;
		backNews.setAll(selectedNews);
	}

	public void saveEditNews() {
		try {
			if (selectedNews.isValid()) {
				newsService.updateNews(selectedNews);
				editMode = false;
			} else {
				sessionManager.addGlobalMessageFatal("News invalid", null);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void cancelEditNews() {
		editMode = false;
		selectedNews.setAll(backNews);
	}

	public void startAddNews() {
		addMode = true;
		newNews = new NewsDto();
	}

	public void addNewNews() throws IOException {
		if (newNews.isValid()) {
			newsService.addNews(newNews);
			addMode = false;
		} else {
			sessionManager.addGlobalMessageFatal("News invalid", null);
		}
	}

	public void cancelAddNews() {
		addMode = false;
		newNews = new NewsDto();
	}

	public List<NewsDto> getFilteredTerms() {
		return filteredTerms;
	}

	public void setFilteredTerms(List<NewsDto> filteredTerms) {
		this.filteredTerms = filteredTerms;
	}
}
